﻿using HirelSeriesH.Tools;
using MySql.Data.MySqlClient;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace HirelSeriesH
{
    public partial class UserCheckForm : UIForm
    {
        public delegate void RefreshDelegate();
        /// <summary>
        /// //改变参数控件状态
        /// </summary>
        public static event RefreshDelegate refreshParams;
        private string roleName;
        private string action;
        public UserCheckForm(string RoleName, string action)
        {
            InitializeComponent();
            this.roleName = RoleName;
            this.action = action;
            if (RoleName == "UserAdmin")
            {
                this.Text = "用户管理员身份验证";
            }
            if (RoleName == "Engineer")
            {
                this.Text = "工程师身份验证";
            }
            if (RoleName == "SuperAdmin")
            {
                this.Text = "Hirel超级管理员";
            }
        }

        private void BtnBack_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            string userName = CbUserName.Items[CbUserName.SelectedIndex].ToString();
            string password = this.TextPassword.Text.Trim();
            if (userName == null || userName == "" || userName == "选择用户名")
            {
                UIMessageDialog.ShowMessageDialog(this, "请选择用户名", UILocalize.ErrorTitle, false, UIStyle.Colorful);
                return;
            }
            if (password == null || password == "")
            {
                UIMessageDialog.ShowMessageDialog(this, "请输入密码", UILocalize.ErrorTitle, false, UIStyle.Colorful);
                return;
            }
            MySqlParameter[] paras = new MySqlParameter[] { new MySqlParameter("@UserName", userName), new MySqlParameter("@PassWord", password), new MySqlParameter("@RoleName", roleName) };
            object result = DBHelper.ExecuteScalar(DBSql.SELECT_SUPERADMIN_USER, 1, paras);
            int count = int.Parse(result.ToString());

            if (count == 0)
            {
                UIMessageDialog.ShowMessageDialog(this, "没有查询到相应的‘" + roleName + "’角色用户", UILocalize.ErrorTitle, false, UIStyle.Colorful);
            }
            else
            {
                
                if (action == "参数配置")
                {
                    ConfigurationForm configurationForm = new ConfigurationForm(false, userName, "cfg");
                    configurationForm.ShowDialog();
                    this.Close();
                }
                if (action == "工厂校准")
                {
                    CorrectForm correctForm = new CorrectForm();
                    correctForm.ShowDialog();
                    this.Close();
                }
                if (action == "出厂设置")
                {
                    SettingsForm settingsForm = new SettingsForm();
                    settingsForm.ShowDialog();
                    this.Close();
                }
                if (action == "用户管理")
                {
                    EngineerUserListForm engineerUserListForm = new EngineerUserListForm(userName);
                    engineerUserListForm.ShowDialog();
                    this.Close();
                }
                if (action == "切换工程师")
                {
                    Global.Global.UserName = userName;
                    Global.Global.UserRole = "Engineer";
                    refreshParams();
                    UIMessageDialog.ShowMessageDialog(this, "当前用户已切换至工程师角色", UILocalize.InfoTitle, false, UIStyle.Colorful);
                    this.Close();
                }
            }

        }
        private void TextPassword_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.btnOk_Click(sender, e);
            }
        }

        private void UserCheckForm_Load(object sender, EventArgs e)
        {
            CbUserName.Items.Add("选择用户名");
            CbUserName.SelectedIndex = 0;
            DataTable dataTable = DBHelper.GetDataTable(DBSql.SELECT_HTRB_USER_USERNAME, 1);
            for (int i = 0; i < dataTable.Rows.Count; i++)
            {
                CbUserName.Items.Add(dataTable.Rows[i]["UserName"].ToString());
            }
        }
    }
}
